package com.byron.pss.web.servlet;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.byron.pss.dao.IUserDAO;
import com.byron.pss.dao.impl.UserDAOImpl;
import com.byron.pss.domain.Const;
import com.byron.pss.domain.User;
import com.byron.pss.util.StringUtils;

/*
 * 用户注册
 */
public class RegisterServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	// 用户操作dao
	private IUserDAO userDao;
	//管理员注册码集合
	private String[] rescode;

	@Override
	public void init() throws ServletException {
		userDao = new UserDAOImpl();
		rescode = getInitParameter("rescode").split(",");
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//首先查看是否有注册码
		String code = req.getParameter("rescode");
		if(!Arrays.asList(rescode).contains(code)){
			//没有注册码，直接返回到注册页面
			req.setAttribute("errorCode","注册码错误");
			req.getRequestDispatcher("/unchecked/register.jsp").forward(req, resp);
			return;
		}
		String username = req.getParameter("username");
		String email = req.getParameter("email");
		String password1 = req.getParameter("password1");
		String password2 = req.getParameter("password2");
		
		// =================验证验证码==============
		if (checkImagecode(req, resp)) {
			// 验证通过
			if (StringUtils.hasLength(username) && StringUtils.hasLength(email)
					&& StringUtils.hasLength(password1)
					&& StringUtils.hasLength(password2)) {
				// 所有的都有值，接下来需要判断的很多，验证邮箱？用户名合法性检验？，暂时只做密码检验
				if (password1.equals(password2)) {
					// 密码一致,调用dao方法注册用户,然后跳转到登陆界面
					User user = new User(null, username, password1, email);
					userDao.save(user);
					resp.sendRedirect(req.getContextPath() + "/unchecked/login.jsp");
					// 跳转后，方法结束
					return;
				} else {
					// 密码不一致
					req.setAttribute("upMsg", "密码不一致");
				}
			} else {
				// 有空值，回到注册页面
				req.setAttribute("upMsg", "所有选项必须填写");
			}
		}
		// 数据回显
		req.setAttribute("username", username);
		req.setAttribute("email", email);
		req.setAttribute("password1", password1);
		req.setAttribute("password2", password2);
		// 从其他连接过来的，显示注册页面
		req.getRequestDispatcher("/unchecked/register.jsp").forward(req, resp);
	}

	/**
	 * 检测验证码是否正确，另外有errorMsg中已经封装了错误信息
	 * @param req 请求信息
	 * @param resp 响应信息
	 * @return true表示成功，false表示错误
	 */
	private boolean checkImagecode(HttpServletRequest req,
			HttpServletResponse resp) {
		// 拿到用户的验证码
		String imagecode = req.getParameter("imagecode");
		// 拿到session中存储的验证码
		String randomcode_in_session = (String) req.getSession().getAttribute(
				Const.RANDOMCODE_IN_SESSION);
		int check = StringUtils.check(imagecode, randomcode_in_session);
		if (check == 1) {
			// 验证不通过，提示验证码输入错误
			req.setAttribute("errorMsg", "验证码输入错误");
			return false;
		}
		if (check == 2) {
			// 返回到页面，并且提示一个错误信息
			req.setAttribute("errorMsg", "验证码不能为空");
			return false;
		}
		return true;
	}

}
